﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCE.UI.Blocks;
using Baosight.ZNH.ZMCE.UI.PopWindows;


namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCE42 : EFViewCmes
    {
        private WorkPlanBlock block = new WorkPlanBlock();
        public ZMCE42()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击生成计划按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public  void OnMakePlanButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                base.OpenEFWindowAlert("请选择一条记录进行计划生成！", true);
                return;
            }
            else if (count > 1)
            {
                base.OpenEFWindowAlert("最多只能选择一条记录进行计划生成！", true);
                return;
            }
            else
            {
                string status = this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString();
                if (!"待处理".Equals(status) && !"已生成工单".Equals(status) && !"作废".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单已处于计划状态，不能重复生成计划！", true);
                    return;
                }
                else if ("作废".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单处于作废状态，不能进行任何操作！", true);
                    return;
                }
                else if ("已生成工单".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单已生成工单，不能生成计划！", true);
                    return;
                }
                else
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock eiBlock = new EIBlock("result");

                    EIColumn column = new EIColumn();
                    column.SetName("fdUseDepartmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("keyID");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdEquipmentCode");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdEquipmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdStatus");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdType");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdSource");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdSourceId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    EIRow row = new EIRow();
                    row.Add("MaintenanceApplykeyID", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("fdUseDepartmentId", this.grid.GetCheckedEIRows()[0]["fdDepartmentId"]);
                    row.Add("fdEquipmentCode", this.grid.GetCheckedEIRows()[0]["fdEquipmentCode"]);
                    row.Add("fdEquipmentId", this.grid.GetCheckedEIRows()[0]["fdEquipmentId"]);
                    row.Add("fdSourceId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("fdSource", "维修申请");
                    row.Add("fdStatus", "计划");
                    row.Add("fdType", "维修");

                    eiBlock.AddRow(row);
                    eiInfo.AddBlock(eiBlock);
                    base.OnInsertButtonClickPopup(eiInfo, "生成计划-新增", "insert", null, sender, e);
                }
            }
        }

        /// <summary>
        /// 点击生成工单按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public  void OnMakeNoteButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录生成工单！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录生成工单！", true);
                return;
            }
            else
            {
                string status = this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString();
                if (!"待处理".Equals(status) && !"已生成计划".Equals(status) && !"作废".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单已生成工单，不能重复生成工单！", true);
                    return;
                }
                else if ("作废".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单处于作废状态，不能进行任何操作！", true);
                    return;
                }
                else if ("已生成计划".Equals(status))
                {
                    base.OpenEFWindowAlert("该申请单已生成计划，不能生成工单！", true);
                    return;
                }
                else
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock eiBlock = new EIBlock("result");

                    EIColumn column = new EIColumn();
                    column.SetName("fdUseDepartmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("keyID");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdEquipmentCode");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdEquipmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdStatus");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdSource");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdSourceId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    EIRow row = new EIRow();
                    row.Add("MaintenanceApplykeyID", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("fdDepartmentId", this.grid.GetCheckedEIRows()[0]["fdDepartmentId"]);
                    row.Add("fdEquipmentCode", this.grid.GetCheckedEIRows()[0]["fdEquipmentCode"]);
                    row.Add("fdEquipmentId", this.grid.GetCheckedEIRows()[0]["fdEquipmentId"]);
                    row.Add("fdPlanId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("fdSource", "维修申请");
                    row.Add("fdStatus", "执行");

                    eiBlock.AddRow(row);
                    eiInfo.AddBlock(eiBlock);
                    OpenNotePopup(eiInfo, "生成工单-新增", "insert", null);
                }
            }
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnCancelButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录要作废的记录！", true);
                return;
            }
            else if (_list.Count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条要作废的记录！", true);
                return;
            }
            else
            {
                if ( _list[0]["fdStatus"].ToString().Equals("作废"))
                {
                    base.OpenEFWindowAlert("该申请单处于作废状态，不能进行任何操作！", true);
                    return;
                }
                else if(_list[0]["fdStatus"].ToString().Equals("已生成计划"))
                {
                    base.OpenEFWindowAlert("该申请单已生成计划，不能作废！", true);
                    return;
                }
                else if (_list[0]["fdStatus"].ToString().Equals("已生成工单"))
                {
                    base.OpenEFWindowAlert("该申请单已生成工单，不能作废！", true);
                    return;
                }
                else
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "设备维修申请-作废", "cancel", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            if (_dialogType.Equals("insert"))
            {
                MakeWorkPlan workPlan = new MakeWorkPlan(_dialogType);//构造一个新的修改页面
                workPlan.EIDataContext = new Platform.Core.UI.EIDataContext();

                _eIInfo.AddBlock(block.getPlanType("cmes.ce.planType", false));
                _eIInfo.AddBlock(block.getPlanStatus("cmes.ce.planStatus", false));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));

                workPlan.EIDataContext.Update(_eIInfo);
                _popup.Content = workPlan;//让新增页面在弹出窗口显示
            }
            else
            {
                MaintenanceApply MaintenanceApply = new MaintenanceApply(_dialogType);//构造一个新的修改页面
                MaintenanceApply.EIDataContext = new Platform.Core.UI.EIDataContext();
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.empName"));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
                if (_dialogType.Equals("cancel"))
                {
                    MaintenanceApply.fdStatus.Value = "作废";
                }
                else
                {
                    if (!_eIInfo.GetBlock("result").Get(0, "fdStatus").Equals("作废"))
                    {
                        MaintenanceApply.fdInvalid.Visibility = Visibility.Collapsed;
                    }

                }
                MaintenanceApply.EIDataContext.Update(_eIInfo);
                _popup.Content = MaintenanceApply;//让新增页面在弹出窗口显示
            }
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);

            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        private void OpenNotePopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            MakeWorkNoteNoPlan note = new MakeWorkNoteNoPlan();//构造一个新的修改页面
            note.EIDataContext = new Platform.Core.UI.EIDataContext();

            NoteBlock noteBlock = new NoteBlock();
            _eIInfo.AddBlock(noteBlock.getNoteType("cmes.ce.noteType", false));
            _eIInfo.AddBlock(noteBlock.getNoteStatus("cmes.ce.noteStatus", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));

            note.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = note;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "维修申请处理-查看", "detail", null, sender, e);
        }
    }
}
